package com.hmdp.utils;

import com.hmdp.service.IkeyService;
import org.fisco.bcos.sdk.BcosSDK;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class UpperChainMutiQueit {

    @Resource
    private IkeyService keyService;

    //获取配置文件路径
    public final String configFile = "src/main/resources/config-example.toml";

    public BcosSDK bcosSDK ;
    public Client client ;
    public CryptoKeyPair cryptoKeyPair ;

    Encryption encryption = new Encryption();

    public UpperChainMutiQueit() {
        //  初始化FISCO BCOS SDK
        bcosSDK = BcosSDK.build(configFile);
        client = bcosSDK.getClient(1);
        cryptoKeyPair = client.getCryptoSuite().createKeyPair();
        client.getCryptoSuite().setCryptoKeyPair(cryptoKeyPair);
    }

    //患者数据进行上链
    public void addPatientOrder(String id, String encryptedName, String encryptedPhone, String encryptedPassword, String encryptedType) throws Exception {

        // 加载智能合约
        AssembleTransactionProcessor transactionProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(client, cryptoKeyPair, "src/main/resources/abi/","");
        // 创建调用交易函数的参数，此处为传入一个参数
        List<Object> params = new ArrayList<>();
        //params.add(encryptedId);
        params.add(id);
        params.add(encryptedName);
        params.add(encryptedPhone);
        params.add(encryptedPassword);
        params.add(encryptedType);

        // 调用智能合约的addOrder方法
        TransactionResponse transactionResponse = transactionProcessor.sendTransactionAndGetResponseByContractLoader("PatientMutiQueries", "0xa7d3ac05c38cb0795674c28b764403efb74bd812", "storePatientData", params);
        // System.out.println("区块链返回的状态值是："+transactionResponse.getTransactionReceipt().getStatus());
        if(transactionResponse!=null){
            String flag =transactionResponse.getTransactionReceipt().getStatus();
            if("0x0".equals(flag)){
                // 获取交易哈希
                String transactionHash = transactionResponse.getTransactionReceipt().getTransactionHash();
                System.out.println("订单上链成功,交易哈希: "+transactionHash);
            }else {
                System.out.println("交易上链失败");
                // 获取错误信息
                String errorMessage = transactionResponse.getTransactionReceipt().getMessage();
                System.out.println("错误信息: " + errorMessage);

            }
        }else{
            System.out.println("交易发送失败");
        }

    }

    //患者查询
    public void getPatientOrder(String id,String privatekey) throws Exception {


        List<Object> params = new ArrayList<>();
        params.add(id);
        // 加载智能合约
        AssembleTransactionProcessor transactionProcessor = TransactionProcessorFactory.createAssembleTransactionProcessor(client, cryptoKeyPair, "src/main/resources/abi/","");
        // 调用智能合约的getOrder方法
        TransactionResponse callResponse = transactionProcessor.sendTransactionAndGetResponseByContractLoader("PatientMutiQueries", "0xa7d3ac05c38cb0795674c28b764403efb74bd812", "getPatientDataById",params);

        //打印返回值
        List<Object>results = callResponse.getReturnObject();

        // 将返回的对象强制转换为 List<Object> 类型
        List<Object> outerList = (List<Object>) results;

        // 遍历外层列表
        for (Object innerObject : outerList) {
            // 将内层对象强制转换为 List<Object> 类型
            List<Object> innerList = (List<Object>) innerObject;
            // 从 innerList 中获取字符串元素
            String patientId = "";
            String decryptedPatientName = "";
            String decryptedPatientPhone = "";
            String decryptedPatientPassword = "";
            String decryptedPatientType = "";
            // 遍历 innerList 并获取对应索引的元素
            for (int i = 0; i < innerList.size(); i++) {
                List<Object> elementList = (List<Object>) innerList.get(i);
                if (elementList.size() > 0) {
                    // System.out.println("elementList里面的数据"+elementList);
                    patientId = (String) elementList.get(0);
                    String PatientName = (String) elementList.get(1);
                    String PatientPhone = (String) elementList.get(2);
                    String PatientPassword = (String) elementList.get(3);
                    String PatientType = (String) elementList.get(4);
                    //解密处理
                  //  Encryption encryption = new Encryption();
                    decryptedPatientName = encryption.rsaDecrypt(PatientName,privatekey);
                    decryptedPatientPhone = encryption.rsaDecrypt(PatientPhone,privatekey);
                    decryptedPatientPassword = encryption.rsaDecrypt(PatientPassword,privatekey);
                    decryptedPatientType= encryption.rsaDecrypt(PatientType,privatekey);
                }
                System.out.println("Patient ID: " + patientId);
                System.out.println("Name: " + decryptedPatientName);
                System.out.println("Phone: " + decryptedPatientPhone);
                System.out.println("Password: " + decryptedPatientPassword);
                System.out.println("Important: " + decryptedPatientType);
                System.out.println("-------------------------");
            }

        }


    }
}
